import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "qrc:/qml/styles" as Styles
import "qrc:/qml/components"
import "resource"

Rectangle {
    id: resourceScreen
    color: Styles.Theme.background

    Component.onCompleted: {
        // 初始化ResourceViewModel
        if (GlobalResourceViewModel && !GlobalResourceViewModel.isInitialized) {
            GlobalResourceViewModel.initialize();
        }
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 0

        // 顶部标题栏
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 80
            color: Styles.Theme.surface

            // 底部边框
            Rectangle {
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                height: 1
                color: Styles.Theme.border
            }

            RowLayout {
                anchors.fill: parent
                anchors.leftMargin: Styles.Theme.spacingLarge
                anchors.rightMargin: Styles.Theme.spacingLarge
                spacing: Styles.Theme.spacingLarge

                // 左侧标题和描述
                ColumnLayout {
                    Layout.fillWidth: true
                    spacing: Styles.Theme.spacingXSmall

                    Text {
                        text: "资源管理"
                        font.pixelSize: Styles.Theme.fontSizeXLarge
                        font.weight: Styles.Theme.fontWeightBold
                        color: Styles.Theme.textPrimary
                    }

                    Text {
                        text: "管理系统中的电梯、自动门、呼叫器和区域占用等资源"
                        font.pixelSize: Styles.Theme.fontSizeNormal
                        color: Styles.Theme.textSecondary
                    }
                }

                // 右侧快速统计
                Row {
                    Layout.alignment: Qt.AlignVCenter
                    spacing: Styles.Theme.spacingLarge

                    // 电梯统计
                    Column {
                        spacing: Styles.Theme.spacingXSmall

                    Text {
                        text: GlobalResourceViewModel ? GlobalResourceViewModel.totalLiftCount : 0
                        font.pixelSize: Styles.Theme.fontSizeLarge
                        font.weight: Styles.Theme.fontWeightBold
                        color: Styles.Theme.primary
                        anchors.horizontalCenter: parent.horizontalCenter
                    }

                        Text {
                            text: "电梯总数"
                            font.pixelSize: Styles.Theme.fontSizeSmall
                            color: Styles.Theme.textSecondary
                            anchors.horizontalCenter: parent.horizontalCenter
                        }
                    }

                    Rectangle {
                        width: 1
                        height: 40
                        color: Styles.Theme.divider
                    }

                    // 区域统计
                    Column {
                        spacing: Styles.Theme.spacingXSmall

                    Text {
                        text: GlobalResourceViewModel ? GlobalResourceViewModel.totalAreaCount : 0
                        font.pixelSize: Styles.Theme.fontSizeLarge
                        font.weight: Styles.Theme.fontWeightBold
                        color: Styles.Theme.success
                        anchors.horizontalCenter: parent.horizontalCenter
                    }

                        Text {
                            text: "区域总数"
                            font.pixelSize: Styles.Theme.fontSizeSmall
                            color: Styles.Theme.textSecondary
                            anchors.horizontalCenter: parent.horizontalCenter
                        }
                    }

                    Rectangle {
                        width: 1
                        height: 40
                        color: Styles.Theme.divider
                    }

                    // 占用统计
                    Column {
                        spacing: Styles.Theme.spacingXSmall

                    Text {
                        text: GlobalResourceViewModel ? GlobalResourceViewModel.occupiedAreaCount : 0
                        font.pixelSize: Styles.Theme.fontSizeLarge
                        font.weight: Styles.Theme.fontWeightBold
                        color: Styles.Theme.warning
                            anchors.horizontalCenter: parent.horizontalCenter
                        }

                        Text {
                            text: "已占用"
                            font.pixelSize: Styles.Theme.fontSizeSmall
                            color: Styles.Theme.textSecondary
                            anchors.horizontalCenter: parent.horizontalCenter
                        }
                    }
                }
            }
        }

        // Tab导航栏
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 50
            color: Styles.Theme.surface

            Row {
                anchors.fill: parent
                anchors.leftMargin: Styles.Theme.spacingLarge
                spacing: 0

                Repeater {
                    model: [
                        { name: "电梯管理", icon: "🛗" },
                        { name: "区域占用", icon: "📍" },
                        { name: "自动门", icon: "🚪" },
                        { name: "呼叫器", icon: "📞" }
                    ]

                    Rectangle {
                        width: 150
                        height: parent.height
                        color: tabBar.currentIndex === index ? Styles.Theme.background : "transparent"

                        // 底部指示条
                        Rectangle {
                            visible: tabBar.currentIndex === index
                            anchors.bottom: parent.bottom
                            anchors.left: parent.left
                            anchors.right: parent.right
                            height: 3
                            color: Styles.Theme.primary
                        }

                        Row {
                            anchors.centerIn: parent
                            spacing: Styles.Theme.spacingSmall

                            Text {
                                text: modelData.icon
                                font.pixelSize: Styles.Theme.fontSizeMedium
                                anchors.verticalCenter: parent.verticalCenter
                            }

                            Text {
                                text: modelData.name
                                font.pixelSize: Styles.Theme.fontSizeNormal
                                font.weight: tabBar.currentIndex === index ? Styles.Theme.fontWeightMedium : Styles.Theme.fontWeightNormal
                                color: tabBar.currentIndex === index ? Styles.Theme.primary : Styles.Theme.textSecondary
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }

                        MouseArea {
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            onClicked: tabBar.currentIndex = index
                            onEntered: {
                                if (tabBar.currentIndex !== index) {
                                    parent.color = Styles.Theme.surfaceVariant
                                }
                            }
                            onExited: {
                                if (tabBar.currentIndex !== index) {
                                    parent.color = "transparent"
                                }
                            }
                        }
                    }
                }
            }

            // 底部边框
            Rectangle {
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                height: 1
                color: Styles.Theme.border
            }
        }

        // Tab内容区域
        StackLayout {
            id: tabBar
            Layout.fillWidth: true
            Layout.fillHeight: true
            currentIndex: 0

            // 电梯管理
            LiftManagementTab {
                id: liftTab
            }

            // 区域占用管理
            AreaManagementTab {
                id: areaTab
            }

            // 自动门管理
            AutoDoorManagementTab {
                id: autoDoorTab
            }

            // 呼叫器管理
            CallerManagementTab {
                id: callerManagementTab
            }
        }
    }

    // 加载指示器
    Rectangle {
        anchors.fill: parent
        color: Qt.rgba(0, 0, 0, 0.5)
        visible: GlobalResourceViewModel ? GlobalResourceViewModel.isLoading : false

        BusyIndicator {
            anchors.centerIn: parent
            running: parent.visible

            contentItem: Item {
                implicitWidth: 64
                implicitHeight: 64

                Rectangle {
                    width: 64
                    height: 64
                    radius: 32
                    color: "transparent"
                    border.width: 4
                    border.color: Styles.Theme.primary
                    opacity: 0.3
                }

                Rectangle {
                    width: 64
                    height: 64
                    radius: 32
                    color: "transparent"
                    border.width: 4
                    border.color: Styles.Theme.primary

                    Rectangle {
                        width: 8
                        height: 8
                        radius: 4
                        color: Styles.Theme.primary
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.top: parent.top
                        anchors.topMargin: 4
                    }

                    RotationAnimation on rotation {
                        running: parent.parent.parent.visible
                        loops: Animation.Infinite
                        from: 0
                        to: 360
                        duration: 1000
                    }
                }
            }
        }

        Text {
            anchors.centerIn: parent
            anchors.verticalCenterOffset: 60
            text: "加载中..."
            font.pixelSize: Styles.Theme.fontSizeNormal
            color: Styles.Theme.textPrimary
        }
    }

    ToastNotification {
        id: operationToast
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: Styles.Theme.spacingXLarge
        width: Math.min(resourceScreen.width - 2 * Styles.Theme.spacingLarge, implicitWidth)
        z: 1000
    }

    Connections {
        target: GlobalResourceViewModel
        ignoreUnknownSignals: true
        function onOperationSuccess(message) {
            operationToast.show(message, "success");
        }
        function onOperationFailed(error) {
            operationToast.show(error, "error");
        }
    }

    // 错误提示
    Rectangle {
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottomMargin: Styles.Theme.spacingLarge
        width: Math.min(parent.width - 2 * Styles.Theme.spacingLarge, 400)
        height: errorText.implicitHeight + 2 * Styles.Theme.spacingNormal
        radius: Styles.Theme.radiusNormal
        color: Styles.Theme.danger
        visible: GlobalResourceViewModel && GlobalResourceViewModel.lastError !== ""
        opacity: 0.95

        Row {
            anchors.fill: parent
            anchors.margins: Styles.Theme.spacingNormal
            spacing: Styles.Theme.spacingNormal

            Text {
                text: "⚠"
                font.pixelSize: Styles.Theme.fontSizeLarge
                color: Styles.Theme.textOnPrimary
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                id: errorText
                text: GlobalResourceViewModel ? GlobalResourceViewModel.lastError : ""
                font.pixelSize: Styles.Theme.fontSizeNormal
                color: Styles.Theme.textOnPrimary
                wrapMode: Text.WordWrap
                width: parent.width - 60
                anchors.verticalCenter: parent.verticalCenter
            }
        }

        // 自动隐藏
        Timer {
            running: parent.visible
            interval: 5000
            onTriggered: {
                if (GlobalResourceViewModel) {
                    GlobalResourceViewModel.clearError();
                }
            }
        }
    }
}
